package com.woniuxy.kaer.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.kaer.system.model.Role;
import com.woniuxy.kaer.system.mapper.RoleMapper;
import com.woniuxy.kaer.system.service.RoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.List;

import static net.sf.jsqlparser.util.validation.metadata.NamedObject.role;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sun
 * @since 2025年02月17日
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
    @Resource
    private RoleMapper roleMapper;

    @Override
    public void addRole(String roleName) throws Exception {
        final QueryWrapper<Role> roleQueryWrapper = new QueryWrapper<>();
        roleQueryWrapper.eq("role_name",roleName);
        if(roleMapper.selectOne(roleQueryWrapper)!=null){
            throw new Exception("已有该角色，请勿重复添加");
        }
        final Role role1 = new Role();
        role1.setRoleName(roleName);
        role1.setState(0);

        // 插入角色到数据库
        roleMapper.insert(role1);
    }

    @Override
    public List<Role> findAllRole() throws Exception {
        // 获取所有角色
        return roleMapper.selectList(null); // 如果是 MyBatis-Plus，可以直接使用 selectList(null) 来查询所有数据
    }
    }



